package sweet.toolbox.command

class CreateProject(val toolboxProps: scala.collection.Map[String, String]) 
  extends ToolboxCommand {

  import java.io._
  import sweet.helper._
  
  import StringHelper.string2helper // implicit string enhancer
  import FileHelper.file2helper // implicit java File enhancer
  import Regex.pattern2regex // implicit java File enhancer
      
  val summary = "Create a new Sweet web application project."
  
  val usage = """
    |Usage: toolbox create-project <projectName>
    |  Create a maven based build project for Sweet web application.
    """.stripMargin
    
  def exec(args: Array[String]){
    if(args.length < 1)
      throw new IllegalArgumentException("Missing project name argument.")
    val projectName = args(0)    
    val project = createToolboxProject(projectName)
    
    createProject(project)
  }
    
  def createToolboxProject(projectName: String): ToolboxProject = {
    val  p = toolboxProps
    val project = new ToolboxProject
    project.name = projectName
    project.templatePath = p("sweet.toolbox.home")+p("toolbox.templates.dir")+p("createProjectTemplate.mavenProjectPath")
    project.packageName = NamesHelper.packageName(projectName)
    project.sweetConfigClassName = NamesHelper.camelCaseName(projectName)+"SweetConfig"
    project.groupId = project.packageName
    project.artifactId = project.packageName
    project.version = p("project.initialVersion")
    project.scalaSrcPath = p("project.scalaSrcPath")
    project.viewPath = p("project.viewPath")
    project.controllerClassPath = p("project.controllerClassPath")+"/"+project.packageName
    project.sweetConfigClassPath = p("project.sweetConfigClassPath")+"/"+project.packageName
    
    project
  }
  
  def createProject(project: ToolboxProject){
    val projectName = project.name
    val fmModel = Map("sweetProject"->project, "toolboxProps"->toolboxProps)
    val templateDir = new File(project.templatePath)
    val destDir = new File("./"+ project.name)
    if(destDir.exists)
      throw new Exception("Project directory "+destDir+" already exist.")
    
    logger.info("Creating project " + projectName)
    logger.debug("Output directory set to " +destDir.getAbsolutePath)
        
    withEachTemplate(templateDir, destDir){ (template, pathname) =>        
      val destFile = createDestinationFile(pathname, destDir){ name=> project.getters(name)().toString }
      val writer = new FileWriter(destFile)
      
      logger.info("Creating file "+destFile.getPathname)
      template.process(fmModel, writer)
    }
    
    logger.debug("Creating project meta file.")
    val dest = new File(destDir, toolboxProjectFilename)
    project.saveTo(dest)
    
    logger.info("Project "+projectName+" created.")
  }  
}


/** Data model that represent a project for ToolboxCommand */
class ToolboxProject extends sweet.helper.FilePropsModel{
  var name: String = _
  var templatePath: String = _
  var packageName: String = _
  var groupId: String = _
  var artifactId: String = _
  var version: String = _
  var scalaSrcPath: String = _
  var viewPath: String = _
  var controllerClassPath: String = _
  var sweetConfigClassName: String = _
  var sweetConfigClassPath: String = _
}

